<?php
define ( 'BASE_ROOT_PATH', dirname ( dirname ( __FILE__ ) ) );
include_once (BASE_ROOT_PATH . "/word/db/DBMySQL.php");
$pageNo = $_REQUEST ['pageNo'];
if (empty ( $pageNo )) {
	$pageNo = 1;
}

$pageSize = $_REQUEST ['pageSize'];
if (empty ( $pageSize )) {
	$pageSize = 10;
}

$start = $pageSize * ($pageNo - 1);

$memberId = $_REQUEST['memberId'];

// 获取问题列表
$db = new DBMySQL ();
$rs = $db->Query ( "select mi.avater,m.nick,q.question_id,q.title,q.member_id,q.content,q.is_anonymous,q.timestamp from question q inner join t_member_info mi on mi.member_id = q.member_id  inner join t_member m on m.member_id = q.member_id order by q.timestamp desc limit $start, $pageSize" );
if (empty ( $rs )) {
	$msg = json_encode ( "没有提问记录" );
	echo '{"code":"0","msg":' . $msg . '}';
	exit ();
}
$dataArray = array ();
$nSize = count ( $rs );
for($i = 0; $i < $nSize; $i ++) {
	$val = $rs [$i];
	$questionId = $val['question_id'];
	$memberId = $val ['member_id'];
	$avater = $val ['avater'];
	$nick = $val ['nick'];
	$title = $val ['title'];
	$questionContent = $val ['content'];
	$timestamp = $val ['timestamp'];
	$isanonymous = $val['is_anonymous'];
	
	// 获取回答列表
	$answersRs = $db->Query ( "select * from question_answer qa join answer a on a.answer_id=qa.answer_id where qa.question_id = $questionId order by a.answer_id desc " );
	$answerCount = count ( $answersRs );
	$answerDataArray = array ();
	for($j = 0; $j < $answerCount; $j ++) {
		$answerVal = $answersRs [$j];
		$introduce = $answerVal ['introduce'];
		$content = $answerVal ['content'];
		$nickname = $answerVal ['nickname'];
		$type = $answerVal ['type'];
		$answerTimestamp = $answerVal['timestamp'];
		$answerDataArray [] = array (
				"introduce" => $introduce,
				"content" => $content,
				"nickname" => $nickname,
				"type" => $type,
				"timestamp" => $answerTimestamp
				
		);
	}
	
	//获取标签列表
	$tagRs = $db->Query("select atag.tag_id as tagid, atag.name as name, btag.tag_id as superid, btag.name as supername from question_tag btag left join question_tag atag on btag.tag_id = atag.super_id join question_in_tag qt on atag.tag_id=qt.tag_id where qt.question_id = $questionId ");
	$tagCount = count($tagRs);
	$tagDataArray = array();
	for($j = 0; $j < $tagCount; $j ++) {
		$tagVal = $tagRs [$j];
		$tagId = $tagVal ['tagid'];
		$tagName = $tagVal ['name'];
		$superTagId = $tagVal ['superid'];
		$superTagName = $tagVal ['supername'];
		$tagDataArray [] = array (
				"tagid" => $tagId,
				"name" => $tagName,
				"superid" => $superTagId,
				"supername" => $superTagName
		);
	}
	
	$examRs = $db->Query("select exam from t_member_info where member_id = $memberId");
	$exam = $examRs[0]['exam'];
	if ($exam == 0 || $exam == 4 || $exam == 5) {
		$exam = "本科";
	}
	else if ($exam == 1) {
		$exam = "研究生";
	}
	else if ($exam == 2) {
		$exam = "商科";
	}
	else if ($exam == 3) {
		$exam = "高中";
	}
	
	$dataArray [] = array (
			'member_id' => $memberId,
			'avater' => $avater,
			'nick' => $nick,
			'title' => $title,
			'content' => $questionContent,
			'timestamp' => $timestamp,
			'isanonymous' => $isanonymous,
			'examTag' => $exam,
			'answers' =>  $answerDataArray,
			'tags' => $tagDataArray
	);
}
$data = json_encode ( $dataArray );

echo '{"code":"0","msg":"","pageNo":"' . $pageNo . '","pageSize":"' . $pageSize . '","total":"' . $nSize . '","data":' . $data . '}';

?>